<!DOCTYPE html>
<uieip type="page" value="login" debug="true" timeout="5000"></uieip>
<uieip type="reg" value="console"></uieip>

<html>

<head>
    <uieip type="file" value="inc" src="/yqwin/easyui/include.txt" cache="true" minute="1440"></uieip>
    <uieip type="file" value="css" src="/yqwin/css/yqwin.min" cache="true"></uieip>
    <uieip type="file" value="js" src="/yqwin/src/yqwin.min" cache="true"></uieip>
    <title>tree edit</title>
</head>

<body style="margin: 0px;">
    <table id="tb_user">
    </table>
    <script>
        var dbname = "yqbase";

        function form_load() {
            $(window).resize(function () {
                onresize();
            });
            onresize();

            $("#tb_user").yqDataGrid("loadData");
        }

        function onresize() {
            $("#tb_user").yqDataGrid('resize', {
                width: $(window).width(),
                height: $(window).height()
            });
        }

        $(function () {
            $.yqDbList({ ajaxUrl: "_db.html" },
                function (data) {
                    if (data.error == 0) {
                        dbname = data.rows[0].name;
                        console.log(data, dbname);
                        init_user();
                    } else {
                        alert(data.info);
                    }
                });
        });

        function init_user() {
            $("#tb_user").yqDataGrid({
                ajaxUrl: "/_db.html",
                dbname: dbname,
                tbname: "hr_user",
                keyId: "sid",
                bindingKeyId: null,
                bindingValue: null,
                pKeyId: null,
                pKeyValue: null,

                addWhere: "",
                order: "name",

                frozenNum: 2,
                editMode: 3,
                editModeW: 600,
                editModeH: 450,
                editModeNo: 1,
                loadData: false,
                isPage: true,

                isEdit: true,
                copy: true,
                add: true,
                upp: true,
                del: true,
                isdel: true,
                isdelEdit: true,
                move: false,

                editLinks: [{}, {
                    id: "login",
                    text: "登录账号",
                    iconCls: "icon2-adp-power",
                }],

                linkField: ["name"],

                uppActionState: function (s_index, s_row, callback) {
                    if (s_row === null) {
                        $("#tb_user").yqDataGrid("btnDisable", "login");
                    } else {
                        $("#tb_user").yqDataGrid("btnEnable", "login");
                    }
                    callback(true);
                },

                btnAction: function (action, params, callback) {
                    if (action == "login") {
                        var row = $("#tb_user").yqDataGrid("getSelected");
                        user_login(row)
                        callback(false);
                    } else {
                        callback(true);
                    }
                },

                beforeBindingTitle: function (columns, callback) {
                    callback(columns);
                }, //标题绑定前
                afterBindingTitle: function () {
                    form_load();
                }, //标题绑定后
            });
        }


        function user_login(row) {
            var prevent_e = $("#tb_user").yqDataGrid("options").prevent_e
            if (prevent_e === undefined) {
                prevent_e = { clientX: 0, clientY: 0 };
            }

            var mdiinfo = $.yqGetWindParams();

            var _pops = {
                prevent_e: prevent_e,
                mdiinfo: mdiinfo,
                ajaxUrl: "",
                titles: login_titles,
                action: "add",

                isEdit: true,
                loadDefault: false,   //加载默认值
                blankTag: "*",       //值不能为空，标题附加标记

                width: 600,
                height: 420,

                callback: function (_row) {
                    saveLoginInfo(_row, function (res) {
                        //console.log(res);
                    });
                },
            };

            var row_ = $.extend({}, row);
            getLoginInfo(row.sid, function (res) {
                row_["action"] = "add";
                if (res.error == 0 && !(res.row === null)) {
                    delete res.row["login_pass"];
                    row_ = $.extend(row_, res.row);
                    row_["action"] = "upp";
                }
                _pops.row = row_;

                if (window.top == window.self) {
                    $.yqAttrPopEdit(_pops);
                }
                else {
                    window.top.$.yqAttrPopEdit(_pops);
                }
            });
        }

        function getLoginInfo(user_id, callback) {
            var opts = {
                ajaxUrl: "/_db.html",
                action: "row",
                dbname: dbname,
                tbname: "hr_user_login",
                keyid: "sid",
                keyval: user_id
            };
            $.yqDbTbRow(opts, function (res) {
                //console.log(res);
                callback(res);
            });
        }

        function saveLoginInfo(row, callback) {
            var action = row["action"]
            delete row["action"];
            var opts = {
                ajaxUrl: "/_db.html",
                action: "action",
                dbname: dbname,
                tbname: "hr_user_login",
                rows: [{ action: action, row: row }],
                fileparm: null                    //文件参数{field_name:"",sto_type:1,sto_name:"12"}
            };
            $.yqDbAction(opts, function (res) {
                var error = 0;
                var info = "";
                if (res.error != 0) {
                    error = res.error;
                    info = res.info;
                } else if (res.rows.length == 0) {
                    error = 1;
                    info = "res.rows.length == 0";
                } else if (res.rows.length > 0) {
                    error = res.rows[0].error;
                    info = res.rows[0].info;
                }
                if (error == 0) {
                    $.yqMessager("success", "登录账号设置成功。");
                } else {
                    $.yqMessager("error", info + "[" + error + "]");
                }
                //console.log(res);
            });
        }

        var login_titles = [
            {
                "name": "name",
                "ui_title": "名称",
                "fd_type": "nvarchar",
                "length": 50,
                "decimal": 0,
                "ui_type": 2,
                "ui_width": 90,
                "ui_visible": true,
                "ui_readonly": true,
                "ui_mask": {
                    "default": "默认值",
                    "min": 0,
                    "max": 50,
                    "blank": true,
                    "format": ""
                },
                group: "用户信息"
            },  {
                "name": "login_name",
                "ui_title": "登录名",
                "fd_type": "nvarchar",
                "length": 50,
                "decimal": 0,
                "ui_type": 2,
                "ui_width": 90,
                "ui_visible": true,
                "ui_readonly": false,
                "ui_mask": {
                    "default": "默认值",
                    "min": 0,
                    "max": 50,
                    "blank": true,
                    "format": ""
                },
                group: "账户信息"
            }, {
                "name": "login_pass",
                "ui_title": "密码",
                "fd_type": "nvarchar",
                "length": 50,
                "decimal": 0,
                "ui_type": 2,
                "ui_width": 90,
                "ui_visible": true,
                "ui_readonly": false,
                "ui_mask": {
                    "default": "默认值",
                    "min": 0,
                    "max": 50,
                    "blank": true,
                    "format": ""
                },
                group: "账户信息"
            }, {
                "name": "phone",
                "ui_title": "手机号",
                "fd_type": "nvarchar",
                "length": 50,
                "decimal": 0,
                "ui_type": 2,
                "ui_width": 90,
                "ui_visible": true,
                "ui_readonly": false,
                "ui_mask": {
                    "default": "默认值",
                    "min": 0,
                    "max": 50,
                    "blank": true,
                    "format": ""
                },
                group: "账户信息"
            }, {
                "name": "email",
                "ui_title": "邮箱",
                "fd_type": "nvarchar",
                "length": 50,
                "decimal": 0,
                "ui_type": 2,
                "ui_width": 90,
                "ui_visible": true,
                "ui_readonly": false,
                "ui_mask": {
                    "default": "默认值",
                    "min": 0,
                    "max": 50,
                    "blank": true,
                    "format": ""
                },
                group: "账户信息"
            }, {
                "is_func": false,
                "name": "uppw",
                "ui_title": "用户登录是否需要更新密码",
                "ui_type": 3,
                "ui_width": 80,
                "ui_visible": true,
                "ui_readonly": false,
                "ui_mask": {},
                group: "账户信息"
            },
        ];

    </script>
</body>

</html>